package com.mhkj.fruit.key.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.joda.time.DateTime;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.mhkj.common.enums.PlanType;
import com.mhkj.common.util.ItemHelper;
import com.mhkj.fruit.key.dao.KeyFruitArchivesDao;
import com.mhkj.fruit.key.entity.KeyFruitArchives;
import com.mhkj.fruit.key.service.KeyFruitArchivesService;
import com.mics.core.common.componet.Pagination;
import com.mics.core.system.base.service.impl.BaseServiceImpl;
import com.mics.core.system.rbac.service.DictionaryService;

@Service
public class KeyFruitArchivesServiceImpl extends BaseServiceImpl<KeyFruitArchives> implements KeyFruitArchivesService {
	@Resource
	private KeyFruitArchivesDao keyFruitArchivesDao;
	@Resource
	private DictionaryService dictionaryService;
	@Resource
	private ItemHelper itemHelper;
	@Resource
	public void setKeyFruitArchivesDao(KeyFruitArchivesDao KeyFruitArchivesDao) {
		super.setBaseRepository(KeyFruitArchivesDao);
	}

	@Override
	public Pagination<KeyFruitArchives> listByPage(KeyFruitArchives KeyFruitArchives, int pageIndex, int size) {
		Specification<KeyFruitArchives> spec = new Specification<KeyFruitArchives>() {
			/**
			 * 序列号
			 */
			private static final long serialVersionUID = -786003485941679959L;

			@Override
			public Predicate toPredicate(Root<KeyFruitArchives> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();
				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];

			//	 Join<Object, Object> fruitType = root.join("fruitType",JoinType.INNER);
			//	 Join<Object, Object> nodeResult = fruitType.join("nodeResult",JoinType.INNER);
			//	 Join<Object, Object> keyType = root.join("keyType",JoinType.INNER);
				 Join<Object, Object> planNode = root.join("planNode",JoinType.INNER);
				 Join<Object, Object> plan = planNode.join("plan",JoinType.INNER);
				 Join<Object, Object> planType = plan.join("planType",JoinType.INNER);
				 

				if (!StringUtils.isEmpty(KeyFruitArchives.getMonth())) {
					DateTime dateTime = new DateTime(KeyFruitArchives.getMonth());
					// 获取传入月份第一天然后减1就是上月最后一天
					Date mini = dateTime.dayOfMonth().withMinimumValue().minusDays(1).toDate();
					// 结束时间大于上月最后一天
					Date max = dateTime.dayOfMonth().withMaximumValue().plusDays(1).toDate();

					lstPredicates.add(criteriaBuilder.between(root.get("createDate").as(Date.class), mini, max));
				}
				
				if (!StringUtils.isEmpty(KeyFruitArchives.getPlanNodeName())) {
					lstPredicates.add(criteriaBuilder.like(planNode.get("nodeName").as(String.class),"%" + KeyFruitArchives.getPlanNodeName() + "%"));
				}
 
				if (!StringUtils.isEmpty(KeyFruitArchives.getKeyType())) {
					if (!KeyFruitArchives.getCode().equals(PlanType.MONTH_TYPE.getCode())) {
						lstPredicates.add(criteriaBuilder.equal(planType.get("id").as(Long.class),KeyFruitArchives.getKeyType().getId()));
					}
				}
				
				if (!KeyFruitArchives.getCode().equals(PlanType.FUNC_TYPE.getCode())) {

					if (!KeyFruitArchives.getCode().equals(PlanType.MONTH_TYPE.getCode())) {
						
						Join<Object, Object> item = plan.join("item", JoinType.INNER);
						if (!StringUtils.isEmpty(KeyFruitArchives.getItemId())) {
							lstPredicates.add( criteriaBuilder.equal(item.get("id").as(Long.class), KeyFruitArchives.getItemId()));
						}

						if (itemHelper.getCurrentUserItems().size() > 0) {
							Predicate p3 = criteriaBuilder.and(item.in(itemHelper.getCurrentUserItems()));
							lstPredicates.add(p3);
						} else {
							Predicate p3 = criteriaBuilder.and(item.isNull());
							lstPredicates.add(p3);
						}
					} 
				}
	
				lstPredicates.add(criteriaBuilder.equal(planNode.get("aduitState").as(Integer.class),2));

				return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
			}
		};
		Page<KeyFruitArchives> pageT = keyFruitArchivesDao.findAll(spec,PageRequest.of(pageIndex, size, Sort.Direction.ASC, "id"));
		Pagination<KeyFruitArchives> pagination = new Pagination<KeyFruitArchives>();
		pagination.setTotal(keyFruitArchivesDao.count(spec));
		pagination.setData(pageT.getContent());
		return pagination;
	}

}
